//
//  CommTools.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/24.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "CommTools.h"

@implementation CommTools

+ (UIViewController *)getCurrentViewController:(id)view{
    UIResponder *next = [view nextResponder];
    do {
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        next = [next nextResponder];
    } while (next != nil);
    return nil;
}

+ (CGSize)getTextSize:(NSString *)text WithWidth:(CGFloat)width {
    return [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
}

+ (UIImage *)image:(UIImage*)image byScalingToSize:(CGSize)targetSize {
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    
    UIGraphicsBeginImageContext(targetSize);
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointZero;
    thumbnailRect.size.width  = targetSize.width;
    thumbnailRect.size.height = targetSize.height;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage ;
}
//判断字符串是否是纯数字
+ (BOOL)isPureNumandCharacters:(NSString *)string
{
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(string.length > 0)
    {
        return NO;
    }
    return YES;
}

+ (void)setUserInfo:(id)responseObject {
    DZInfos *userInfos = [DZInfos mj_objectWithKeyValues:responseObject[@"data"][@"uInfo"]];
    [theUser setObject:userInfos.mj_keyValues forKey:@"DZUserInfomation"];
    DZUserInfo.userInfo = userInfos;
    NSArray *banksArr = DZUserInfo.userInfo.banks;
    if ([userInfos.is_transaction_pwd integerValue] == 0) {
        [theUser setObject:[NSNumber numberWithBool:NO] forKey:ISSETDEALPWD];
    } else {
        [theUser setObject:[NSNumber numberWithBool:YES] forKey:ISSETDEALPWD];
    }
    if (banksArr != nil) {
        if (!banksArr.count) {
            [theUser setObject:[NSNumber numberWithBool:NO] forKey:ISBINDCARD];
            [theUser setObject:[NSNumber numberWithBool:NO] forKey:ISSETBANK];
        } else {
            [theUser setObject:[NSNumber numberWithBool:YES] forKey:ISBINDCARD];
            NSString *bankBranch = [banksArr firstObject][@"bank_province"];
            if (bankBranch.length) {
                [theUser setObject:[NSNumber numberWithBool:YES] forKey:ISSETBANK];
            } else {
                [theUser setObject:[NSNumber numberWithBool:NO] forKey:ISSETBANK];
            }
        }
    } else {
        [theUser setObject:[NSNumber numberWithBool:NO] forKey:ISBINDCARD];
        [theUser setObject:[NSNumber numberWithBool:NO] forKey:ISSETBANK];
    }
    [theUser synchronize];
}

+ (void)checkIndex:(UIViewController *)vc result:(void(^)(id result))block {
    DZSHOWHUD;
    [CommTools resetRequestWithURL:nil andParams:nil httpMethod:nil complection:^(id result) {
        DZHIDEHUD;
        block(result);
        if ([result[ISLOGIN] boolValue] == NO) {
            [SVProgressHUD dismiss];
            if (userValue(HOSTTEL)) {
                LoginViewController *login = [[LoginViewController alloc] init];
                login.telNum = userValue(HOSTTEL);
                [vc.navigationController pushViewController:login animated:YES];
            } else {
                RegisterViewController *join = [[RegisterViewController alloc] init];
                [vc.navigationController pushViewController:join animated:YES];
            }
        }
        [SVProgressHUD dismiss];
    }];
}

+ (void)resetRequestWithURL:(NSString *)url andParams:(NSDictionary *)params httpMethod:(NSString *)method complection:(void(^)(id result))block {
    
}

+ (void)getUserInfo:(void (^)(id responst))block {
    __block NSInteger rns_expire = NSIntegerMax;
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.httpMethod = @"GET";
    request.requestUrl = @"index/index";
    [DZNetAPI request:request success:^(id response) {
        NSLog(@"%@",response);
        DZResponse *model = [DZResponse mj_objectWithKeyValues:response];
        if (model.rt == 1) {
            DZRnsAndSid *rns = [DZRnsAndSid mj_objectWithKeyValues:model.data];
            if (rns.rns) {
                [theUser setObject:rns.rns forKey:RNS];
            }
            if (rns.sid) {
                [theUser setObject:rns.sid forKey:SID];
            }
            rns_expire = rns.rns_expire;
        }
        if (rns_expire == 0 && DZUserInfo.userInfo.mobile) {
            DZRequestUtil *request = [[DZRequestUtil alloc] init];
            request.requestUrl = @"Index/login";
            request.params = @{@"mobile":DZUserInfo.userInfo.mobile};
            [DZNetAPI request:request success:^(id response) {
                NSLog(@"%@",response);
                DZResponse *info = [DZResponse mj_objectWithKeyValues:response];
                if (info.rt == 1) {
                    [CommTools setUserInfo:response];
                    block(response);
                    if (info.data[@"uInfo"][LOGIN_TOKEN]) {
                        [theUser setObject:info.data[@"uInfo"][LOGIN_TOKEN] forKey:LOGIN_TOKEN];
                    }
                    [theUser setObject:[NSNumber numberWithBool:YES] forKey:ISLOGIN];
                }else {
                    block(response);
                    [CommTools removeAllSet];
                    [theUser setObject:[NSNumber numberWithBool:NO] forKey:ISLOGIN];
                }
            } failure:^(NSError *error) {
            }];
        }
        if (rns_expire == 1) {
            [theUser setObject:[NSNumber numberWithBool:YES] forKey:ISLOGIN];
            DZRequestUtil *request = [[DZRequestUtil alloc] init];
            request.requestUrl = @"User/getUserInfo";
            request.httpMethod = @"GET";
            [DZNetAPI request:request success:^(id response) {
                NSLog(@"%@",response);
                DZResponse *info = [DZResponse mj_objectWithKeyValues:response];
                if (info.rt == 1) {
                    [CommTools setUserInfo:response];
                }else{
                    block(response);
                }
            } failure:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
        [theUser synchronize];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}


+ (void)removeAllSet {
    [theUser removeObjectForKey:LOGIN_TOKEN];
    [theUser removeObjectForKey:SID];
    [theUser removeObjectForKey:RNS];
    [theUser removeObjectForKey:HKNAME];
    [theUser setObject:[NSNumber numberWithBool:NO] forKey:ISLOGIN];
    [theUser removeObjectForKey:@"DZUserInfomation"];
    DZUserInfo.userInfo = nil;
    [theUser setObject:[NSNumber numberWithInteger:0] forKey:HAVEGESTUREPWD];
    [theUser synchronize];
    [self getRnsAndSid:^(id result) {
    }];
}


+ (void)getRnsAndSid:(void (^)(id))block {
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.requestUrl = @"index/index";
    [DZNetAPI request:request success:^(id result) {
        NSLog(@"%@",result);
        DZResponse *model = [DZResponse mj_objectWithKeyValues:result];
        if (model.rt == 1) {
            DZRnsAndSid *rns = [DZRnsAndSid mj_objectWithKeyValues:model.data];
            if (rns.rns) {
                [theUser setObject:rns.rns forKey:RNS];
            }
            if (rns.sid) {
                [theUser setObject:rns.sid forKey:SID];
            }
            [theUser synchronize];
            block(result);
        }
    } failure:^(NSError *error) {
    }];
}

@end
